<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

/**
 * SectionsController
 * 
 * @package JL Panel
 * @author JLQ
 * @copyright 2009
 * @version 1.0
 * @access public
 */
class SectionsController extends JLControllerBase {

    private $config = null;
    private $path = "Sections";
    private $title = "Système d'Actualisation CSME > Les Sections";
    private $data = null;

    /**
     * Funcion por defecto para los controladores
     * @Retorna No
     * @return
     */
    public function Index() {
        require JLPATH_MODELS . DS . "SectionModel.php";
        $section = new SectionModel();
        $data['sections'] = $section->getSections();
        $data['texttitle'] = " > Les Sections";

        require JLPATH_MODELS . DS . "SectionViewModel.php";
        $sectionView = new SectionViewModel();

        $id = $this->getIdSection();

        $sectView = $sectionView->getSectionView($id);
        $data['sectView'] = $sectView;

        $data['classcurrent3'] = ' class="current"';
        $this->View->Show($this->path, $this->title, $data);
    }

    function Membre() {
        require JLPATH_MODELS . DS . "SectionModel.php";
        $section = new SectionModel();
        $data['sections'] = $section->getSections();
        $data['texttitle'] = " > Sections";

        require JLPATH_MODELS . DS . "StateModel.php";
        $state = new StateModel();
        $states = $state->getStates();
        $data['states'] = $states;
        $id = $this->getIdSection();
        require JLPATH_MODELS . DS . "MembresModel.php";
        $membres = new MembresModel();
        $membre = $membres->getMembresByIdSectionsView($id);
        $data['membre'] = $membre;
        $data['classcurrent3'] = ' class="current"';
        $this->View->Show("Membre", $this->title, $data);
    }

    function Contacts() {
        require JLPATH_MODELS . DS . "SectionModel.php";
        $section = new SectionModel();
        $data['sections'] = $section->getSections();
        $data['texttitle'] = " > Sections";

        require JLPATH_MODELS . DS . "StateModel.php";
        $state = new StateModel();
        $states = $state->getStates();
        $data['states'] = $states;
        $id = $this->getIdSection();

        $data['classcurrent3'] = ' class="current"';
        $this->View->Show("Contacts", $this->title, $data);
    }

    function Liens() {
        require JLPATH_MODELS . DS . "SectionModel.php";
        $section = new SectionModel();
        $data['sections'] = $section->getSections();
        $data['texttitle'] = " > Sections";

        require JLPATH_MODELS . DS . "StateModel.php";
        $state = new StateModel();
        $states = $state->getStates();
        $data['states'] = $states;
        $id = $this->getIdSection();

        $data['classcurrent3'] = ' class="current"';
        $this->View->Show("Liens", $this->title, $data);
    }

    function Photos() {
        require JLPATH_MODELS . DS . "SectionModel.php";
        $section = new SectionModel();
        $data['sections'] = $section->getSections();
        $data['texttitle'] = " > Sections";

        require JLPATH_MODELS . DS . "StateModel.php";
        $state = new StateModel();
        $states = $state->getStates();
        $data['states'] = $states;
        $id = $this->getIdSection();

        $data['classcurrent3'] = ' class="current"';
        $this->View->Show("Photos", $this->title, $data);
    }

    /**
     * SectionsController>SavePresentation()
     * 
     * @return
     */
    function SavePresentation() {
        $int_id_sections_view = $_POST['id'];
        $txt_desc_presentation = $_POST['txtpresentation'];

        require JLPATH_MODELS . DS . "SectionViewModel.php";
        $sectionView = new SectionViewModel();
        $_true = $sectionView->UpdatePresentation($int_id_sections_view, $txt_desc_presentation);
        if ($_true)
            echo "Données sauvegarder avec succès.";
        else
            echo "La mise à jour des données est valide.";
    }

    /**
     * SectionsController>SaveAdhesion()
     * 
     * @return
     */
    function SaveAdhesion() {
        $int_id_sections_view = $_POST['id'];
        $txt_desc_adhesion = $_POST['_txt_desc_adhesion'];

        require JLPATH_MODELS . DS . "SectionViewModel.php";
        $sectionView = new SectionViewModel();
        $_true = $sectionView->UpdateAdhesion($int_id_sections_view, $txt_desc_adhesion);
        if ($_true)
            echo "Données sauvegarder avec succès.";
        else
            echo "La mise à jour des données est valide.";
    }

    /**
     * SectionsController>insertMembre()
     * 
     * @return
     */
    function insertMembre() {
        $var_photo = trim($_POST['var_photo']);
        $var_noms = $_POST['var_noms'];
        $var_status = $_POST['var_status'];
        $var_dates = $_POST['var_dates'];
        $int_mem_order = $_POST['int_mem_order'];
        $int_id_sections_view = $_POST['int_id_sections_view'];

        require JLPATH_MODELS . DS . "MembresModel.php";
        $Membres = new MembresModel();
        $_true = $Membres->insertMembre($var_photo, $var_noms, $var_status, $var_dates, $int_mem_order, $int_id_sections_view);

        $jl = new JLTools();
        $pathImg = JLPATH_WEB_SITE . DS . "images" . DS . "membres" . DS . $var_photo;
        $image = $jl->creaImagen($pathImg, $var_photo);
        $jl->saveImagen($image, $pathImg, 100, 126, 126);

        if ($_true)
            echo "Données sauvegarder avec succès.";
        else
            echo "";
    }

    /**
     * SectionsController>deleteMembre()
     * 
     * @return
     */
    function deleteMembre() {
        $id = $_POST['_idmembre'];

        require JLPATH_MODELS . DS . "MembresModel.php";
        $Membres = new MembresModel();
        $membre = $Membres->getMembresById($id);

        $membrepath = JLPATH_WEB_SITE . DS . "images" . DS . "membres" . DS . $membre['var_photo'];
        if (is_file($membrepath))
            @unlink($membrepath);

        $_true = $Membres->deleteMembre($id);
        if ($_true)
            echo "Le membre a été supprimé";
        else
            echo "Erreur suppression dossier";
    }

    function getIdSection() {
        if ($_SESSION['_int_id_typeuser'] == 1) {
            if ($this->isSetNoEmpty($_GET['id']))
                $id = intval($_GET['id']);
            else
                $id = 1;
        }
        else
            $id = $_SESSION['_int_id_sections_view'];

        return $id;
    }

    function Adhesion() {
        require JLPATH_MODELS . DS . "SectionModel.php";
        $section = new SectionModel();
        $data['sections'] = $section->getSections();
        $data['texttitle'] = " > Sections";

        require JLPATH_MODELS . DS . "SectionViewModel.php";
        $sectionView = new SectionViewModel();

        $id = $this->getIdSection();
        $sectView = $sectionView->getSectionView($id);
        $data['sectView'] = $sectView;

        require JLPATH_MODELS . DS . "StateModel.php";
        $state = new StateModel();
        $states = $state->getStates();
        $data['states'] = $states;

        require JLPATH_MODELS . DS . "AdhesionModel.php";
        $adhesions = new AdhesionModel();
        $adhesion = $adhesions->getAdhesionByIdSectionsView($id);
        $data['adhesion'] = $adhesion;

        $data['classcurrent3'] = ' class="current"';
        $this->View->Show("Adhesion", $this->title, $data);
    }

}